package com.wx.wxapp.service;

import com.wx.wxapp.controller.OSSClientUtil;
import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.*;
import com.wx.wxapp.exception.ImgException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ImageService {
    public static final Logger logger = LoggerFactory.getLogger(ImageService.class);

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private OSSClientUtil ossClientUtil;

    private String imagePath = "/Users/xuyifan/Desktop/image";


    /*
     *   功能：上传用户发帖图片到服务器上
     *   参数：
     *       file:图片流
     *       cardId:帖子的id
     *       cardType:帖子类型
     * */
    @Transactional
    public void UploadImage(MultipartFile file,String cardId,String cardType){
        if (file == null || file.getSize() <= 0) {
            throw new ImgException("file不能为空");
        }
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        String name = ossClientUtil.uploadImg2Oss(file);
        String imageUrl = ossClientUtil.getImgUrl(name);
        if (cardType.equals("food")){
            FoodimageMapper foodimageMapper = sqlSession.getMapper(FoodimageMapper.class);
            Foodimage foodimage = new Foodimage();
            foodimage.setImagename(imageUrl);
            foodimage.setCardid(cardId);
            foodimageMapper.insert(foodimage);
            sqlSession.close();
        }else if (cardType.equals("disease")){
            DiseaseimageMapper diseaseimageMapper = sqlSession.getMapper(DiseaseimageMapper.class);
            Diseaseimage diseaseimage = new Diseaseimage();
            diseaseimage.setImagename(imageUrl);
            diseaseimage.setCardid(cardId);
            diseaseimageMapper.insert(diseaseimage);
            sqlSession.close();
        }
    }

    /*
     *   功能：上传用户宠物图片到服务器上
     *   参数：
     *       file:图片流
     *       openid:用户的openid
     * */
    @Transactional
    public String PetUploadImage(MultipartFile file,String openid) throws ImgException {
        if (file == null || file.getSize() <= 0) {
            throw new ImgException("file不能为空");
        }
        OSSClientUtil ossClient=new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(file);
        String imgUrl = ossClient.getImgUrl(name);
        System.out.println("imgUrl"+imgUrl);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int userId = userMapper.selectByOpenid(openid).getId();
        Petimage petimage = new Petimage();
        petimage.setImagename(imgUrl);petimage.setUserid(userId);
        petimageMapper.insert(petimage);
        sqlSession.close();
        return imgUrl;
    }


    /*
     *   功能：获取用户宠物图片
     *   参数：
     *       openid:用户的openid
     * */
    @Transactional
    public List<Petimage> GetPetImage(String openid){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int userId = userMapper.selectByOpenid(openid).getId();
        List<Petimage> petimages = petimageMapper.selectByUserId(userId);
        sqlSession.close();
        return petimages;
    }

    /*
     *   功能：在用户更改宠物图片时，删除之前保存的图片
     *   参数：
     *       openid:用户的openid
     * */
    @Transactional
    public void DeleteImage(String openid){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int id = userMapper.selectByOpenid(openid).getId();
        System.out.println("id"+ id);
        petimageMapper.deleteByUserId(id);
        sqlSession.close();
    }

    /*
     *   功能：获取帖子图片
     *   参数：
     *       cardId:帖子的Id
     *       cardType:帖子类型
     * */
    @Transactional
    public List<String> GetCardImage(String cardId,String cardType){
        List<String> imageUrls = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        if (cardType.equals("food")){
            FoodimageMapper foodimageMapper = sqlSession.getMapper(FoodimageMapper.class);
            List<Foodimage> foodimages = foodimageMapper.selectByCardId(cardId);
            Iterator<Foodimage> it1 = foodimages.iterator();
            while (it1.hasNext()){
                Foodimage foodimage = it1.next();
                imageUrls.add(foodimage.getImagename());
            }
        }else if (cardType.equals("disease")){
            DiseaseimageMapper diseaseimageMapper = sqlSession.getMapper(DiseaseimageMapper.class);
            List<Diseaseimage> diseaseimages = diseaseimageMapper.selectByCardId(cardId);
            Iterator<Diseaseimage> it1 = diseaseimages.iterator();
            while (it1.hasNext()){
                Diseaseimage diseaseimage = it1.next();
                imageUrls.add(diseaseimage.getImagename());
            }
        }
        sqlSession.close();
        return imageUrls;
    }
}
